/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginDialog.java
 *
 * Created on May 9, 2010, 11:55:25 AM
 */

package org.foment.gem.dialogs;

import javax.swing.JOptionPane;
import static org.foment.joliage3.core.internationalization.Internationalization.i18n;
import java.sql.*;
import org.foment.gem.Integrator;
import org.foment.util.Md5Generator;
import org.foment.joliage3.sql.*;

/**
 *
 * @author Admin
 */
public class LoginDialog extends javax.swing.JDialog {

    /** Creates new form LoginDialog */
    public LoginDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setVisible(true);
        toFront();
        this.getRootPane().setDefaultButton(jButton1);
        parent.setEnabled(false);
        
    }

    public void skipLogging()
    {
        Integrator._user.setUserName("test");
        Integrator._user.setPrivAllAccess(true);
        Integrator._user.setPrivReadOnly(true);
        Integrator._user.notifyUserLoggedIn();
        getParent().setEnabled(true);
        dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldUsername = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldPassword = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Ulogujte se");

        jLabel1.setText(i18n("Enter your username and password"));

        jLabel2.setText(i18n("Username"));

        jLabel3.setText(i18n("Password"));

        jButton1.setText(i18n("Log in"));
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(i18n("Close program"));
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldPassword)
                            .addComponent(jTextFieldUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

   
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked

         
        // get username and pass and check if they are not empty
        String uname = jTextFieldUsername.getText().trim();
        String upass = new String(jTextFieldPassword.getPassword()).trim();

        // check if uname and pass are enetered and display error message
        if (uname.equals("") || upass.equals("")) {

            String message = i18n("You didn't enter username or password!");
            String[] options = {i18n("Yes")};

            jOptionPane.showOptionDialog(this, message, "Foment Group", jOptionPane.OK_OPTION, jOptionPane.ERROR_MESSAGE, null, options, 0);

        } else {

            // check if user exists in database and its uname and pass are correct
            String query = "SELECT * FROM user WHERE UserUsername = '"+ uname +"' AND UserPassword = '"+ Md5Generator.md5(upass) +"';";
            try
            {
                Connection conn = Database.getConnection();
                ResultSet rs = conn.createStatement().executeQuery(query);
                int rowCount = 0;

                if (rs != null)
                {

                    while(rs.next()) {

                        rowCount++;

                        // read basic data about this user
                        // userFullName and privileges
                        Integrator._user.setID(rs.getInt("UserID"));
                        Integrator._user.setFullName(rs.getString("UserFullName"));
                        Integrator._user.setUserName(uname);
                        Integrator._user.setPrivAllAccess(rs.getString("UserPriv_AllAccess").equals("1"));
                        Integrator._user.setPrivReadOnly(rs.getString("UserPriv_ReadOnly").equals("1"));

                        Integrator._user.notifyUserLoggedIn();
                        //System.out.println(rs.getString("UserPriv_AllAccess"));
                        //System.out.println("Uspesno ste se ulogovali!");

                        // enable all disabled panels in integrator
                        getParent().setEnabled(true);

                        // hide this frame
                        dispose();
                        break;
                    }
                }

                if (rowCount == 0) {

                    String message = i18n("Wrong username or password entered!");
                    String[] options = {i18n("Yes")};

                    jOptionPane.showOptionDialog(this, message, "Foment Group", jOptionPane.OK_OPTION, jOptionPane.ERROR_MESSAGE, null, options, 0);
                }

                rs.close();
                conn.close();

            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked

        System.exit(0);
        //skipLogging();
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        jButton1MouseClicked(null);
        
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jTextFieldPassword;
    private javax.swing.JTextField jTextFieldUsername;
    // End of variables declaration//GEN-END:variables

    private JOptionPane jOptionPane;
}
